package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Attendance;
import cn.wolfcode.crm.mapper.AttendanceMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IAttendanceService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class AttendanceServiceImpl implements IAttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;

    //保存和更新员工操作
    public int saveOrUpdate(Attendance record) {
        if (record.getId() == null) {
            record.setSignInDate(new Date());
            record.setSignInTime(new Date());
            return attendanceMapper.insert(record);
        } else {
            record.setSignOutTime(new Date());
            return attendanceMapper.updateByPrimaryKey(record);
        }
    }

    //查询单个员工操作
    public Attendance selectByPrimaryKey(Long id) {
        return attendanceMapper.selectByPrimaryKey(id);
    }


    //员工列表操作
    public List<Attendance> selectAll() {
        return attendanceMapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int count = attendanceMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Attendance> data = attendanceMapper.queryList(qo);
        return new PageResult(count, data);
    }

    public void changeState(Long id) {
        attendanceMapper.changeState(id);
    }


    public Attendance selectBySignInDate(String date) {
        return attendanceMapper.selectBySignInDate(date);
    }

    public void updatePatchTime(Attendance attendance) {
        attendanceMapper.updatePatchTime(attendance);
    }

    public Boolean selectSignInStateByDate(Date signInDate) {
        return attendanceMapper.selectSignInStateByDate(signInDate);
    }
}
